-- Unity事件系统适配器
-- 负责将Lua Battle System的事件系统与Unity C#回调系统连接

local LuaBridge = require("lua_bridge")
local EventSystem = require("utils.event_system")
local Logger = require("utils.logger")

local UnityEventAdapter = {}

-- 事件映射表
local event_mapping = {
    ["battle_start"] = "on_battle_start",
    ["battle_end"] = "on_battle_end",
    ["turn_start"] = "on_turn_start",
    ["turn_end"] = "on_turn_end",
    ["character_action"] = "on_action_execute",
    ["character_damage"] = "on_character_damage",
    ["character_heal"] = "on_character_heal",
    ["character_death"] = "on_character_death",
    ["skill_cast"] = "on_skill_effect",
    ["item_use"] = "on_item_use",
    ["status_effect_applied"] = "on_status_effect",
    ["status_effect_removed"] = "on_status_effect"
}

-- 初始化事件适配器
function UnityEventAdapter.init()
    Logger.info("初始化Unity事件适配器...")
    
    -- 注册所有Lua事件监听器
    for lua_event, unity_event in pairs(event_mapping) do
        EventSystem:register(lua_event, function(...)
            UnityEventAdapter.handle_lua_event(lua_event, unity_event, ...)
        end, 100) -- 设置高优先级确保Unity回调优先执行
    end
    
    Logger.info("Unity事件适配器初始化完成")
end

-- 处理Lua事件并转发给Unity
function UnityEventAdapter.handle_lua_event(lua_event_name, unity_event_name, ...)
    Logger.debug("处理Lua事件: " .. lua_event_name .. " -> " .. unity_event_name)
    
    -- 根据事件类型处理数据
    if lua_event_name == "battle_start" then
        UnityEventAdapter.handle_battle_start(...)
    elseif lua_event_name == "battle_end" then
        UnityEventAdapter.handle_battle_end(...)
    elseif lua_event_name == "turn_start" then
        UnityEventAdapter.handle_turn_start(...)
    elseif lua_event_name == "character_action" then
        UnityEventAdapter.handle_character_action(...)
    elseif lua_event_name == "character_damage" then
        UnityEventAdapter.handle_character_damage(...)
    elseif lua_event_name == "character_heal" then
        UnityEventAdapter.handle_character_heal(...)
    elseif lua_event_name == "character_death" then
        UnityEventAdapter.handle_character_death(...)
    elseif lua_event_name == "skill_cast" then
        UnityEventAdapter.handle_skill_cast(...)
    elseif lua_event_name == "status_effect_applied" then
        UnityEventAdapter.handle_status_effect_applied(...)
    elseif lua_event_name == "status_effect_removed" then
        UnityEventAdapter.handle_status_effect_removed(...)
    else
        -- 通用事件处理
        LuaBridge.trigger_unity_callback(unity_event_name, ...)
    end
end

-- 处理战斗开始事件
function UnityEventAdapter.handle_battle_start(battle_id, team1, team2)
    local team1_data = LuaBridge.create_team_data(team1)
    local team2_data = LuaBridge.create_team_data(team2)
    
    LuaBridge.trigger_unity_callback("on_battle_start", battle_id, team1_data, team2_data)
end

-- 处理战斗结束事件
function UnityEventAdapter.handle_battle_end(battle_id, result)
    local result_data = LuaBridge.create_battle_result_data(result)
    
    LuaBridge.trigger_unity_callback("on_battle_end", battle_id, result_data)
end

-- 处理回合开始事件
function UnityEventAdapter.handle_turn_start(battle_id, turn_number)
    LuaBridge.trigger_unity_callback("on_turn_start", battle_id, turn_number)
end

-- 处理角色行动事件
function UnityEventAdapter.handle_character_action(character, decision, battle_state)
    local character_data = LuaBridge.create_character_data(character)
    local decision_data = LuaBridge.create_decision_data(decision)
    
    LuaBridge.trigger_unity_callback("on_action_execute", character_data, decision_data)
end

-- 处理角色受伤事件
function UnityEventAdapter.handle_character_damage(character, damage, new_hp, damage_type)
    local character_id = character.id
    local damage_amount = damage or 0
    local current_hp = new_hp or character.hp
    
    LuaBridge.trigger_unity_callback("on_character_damage", character_id, damage_amount, current_hp)
end

-- 处理角色治疗事件
function UnityEventAdapter.handle_character_heal(character, heal_amount, new_hp)
    local character_id = character.id
    local heal = heal_amount or 0
    local current_hp = new_hp or character.hp
    
    LuaBridge.trigger_unity_callback("on_character_heal", character_id, heal, current_hp)
end

-- 处理角色死亡事件
function UnityEventAdapter.handle_character_death(character, death_reason)
    local character_id = character.id
    
    LuaBridge.trigger_unity_callback("on_character_death", character_id, death_reason or "unknown")
end

-- 处理技能施放事件
function UnityEventAdapter.handle_skill_cast(caster, skill, target, effect_result)
    local caster_id = caster.id
    local skill_id = skill.id
    local target_id = target and target.id or ""
    
    LuaBridge.trigger_unity_callback("on_skill_effect", caster_id, skill_id, target_id)
end

-- 处理状态效果施加事件
function UnityEventAdapter.handle_status_effect_applied(character, effect_name, effect_data)
    local character_id = character.id
    
    LuaBridge.trigger_unity_callback("on_status_effect", character_id, effect_name, true)
end

-- 处理状态效果移除事件
function UnityEventAdapter.handle_status_effect_removed(character, effect_name)
    local character_id = character.id
    
    LuaBridge.trigger_unity_callback("on_status_effect", character_id, effect_name, false)
end

-- 手动触发Unity回调（用于测试）
function UnityEventAdapter.test_unity_callback(event_name, ...)
    Logger.info("测试Unity回调: " .. event_name)
    LuaBridge.trigger_unity_callback(event_name, ...)
end

-- 获取事件映射表
function UnityEventAdapter.get_event_mapping()
    return event_mapping
end

-- 添加新的事件映射
function UnityEventAdapter.add_event_mapping(lua_event, unity_event)
    event_mapping[lua_event] = unity_event
    
    -- 注册新的事件监听器
    EventSystem:register(lua_event, function(...)
        UnityEventAdapter.handle_lua_event(lua_event, unity_event, ...)
    end, 100)
    
    Logger.info("添加新事件映射: " .. lua_event .. " -> " .. unity_event)
end

-- 移除事件映射
function UnityEventAdapter.remove_event_mapping(lua_event)
    if event_mapping[lua_event] then
        event_mapping[lua_event] = nil
        Logger.info("移除事件映射: " .. lua_event)
    end
end

-- 导出模块
return UnityEventAdapter